package edu.cuit.campus.portal.controller;

import com.alibaba.fastjson2.JSONObject;
import edu.cuit.campus.commons.StringUtils;
import edu.cuit.campus.commons.annotations.Controller;
import edu.cuit.campus.commons.annotations.RequestMapping;
import edu.cuit.campus.commons.annotations.ResponseBody;
import edu.cuit.campus.commons.response.Result;
import edu.cuit.campus.commons.response.ServiceStatus;
import edu.cuit.campus.entity.mapper.StudentEntityMapper;
import edu.cuit.campus.entity.po.Student;
import edu.cuit.campus.entity.vo.StudentView;
import edu.cuit.campus.portal.service.StudentService;
import edu.cuit.campus.portal.service.bean.StudentServiceBean;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.BufferedReader;
import java.io.IOException;


/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/8/7 11:24
 */
@Controller
@RequestMapping("/student/")
public class StudentController {
    private StudentService service = new StudentServiceBean();

    /**
     * 求职者的注册请求接口
     * method: post
     * parameter type: json
     * {"username": "peppa", "password": "886887"}
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @return 统一响应格式
     */
    @RequestMapping("/register")
    @ResponseBody
    public Result register(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //1.接收用户请求
        BufferedReader reader = request.getReader();
        String json = "";
        String line = null;
        while((line = reader.readLine()) != null)
            json += line;
        //将json格式字符串解析成Student的实例对象
        if(!StringUtils.isBlank(json))
            return service.register(JSONObject.parseObject(json, Student.class));
        return Result.fail("注册信息不完善....");
    }

    @RequestMapping("/login")
    @ResponseBody
    public Result login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        var username = request.getParameter("username");
        var password = request.getParameter("password");
        var result = service.login(username, password);
        if(result.status().equals(ServiceStatus.SUCCESS)){
            request.getSession().setAttribute("user", result.data());
            var mapper = StudentEntityMapper.INSTANCE;
            var studentView = mapper.studentToStudentView((Student) result.data());

            return Result.ok(studentView);
        }
        return result;
    }

    @RequestMapping("/active")
    @ResponseBody
    public Result active(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        var username = request.getParameter("username");
        var code = Integer.valueOf(request.getParameter("code"));
        var result = service.active(username, code);
        return result ? Result.ok() : Result.fail();
    }
}
